<?php
/**
 * Created by PhpStorm.
 * User: 吕金鹏
 * Date: 2021/5/21
 * Time: 10:32 上午
 */

namespace app\index\controller;


use think\Db;
use think\facade\Cookie;
use think\facade\Request;

class WebConfig extends Base
{
    //站点设置
    public function siteConfig()
    {
        $where['aid'] = $this->admin['id'];
        $data = findData('siteconfig',$where);
        $this->assign('data',$data);
        return $this->fetch('siteConfig');
    }

    // 修改站点
    public function editConfig()
    {
        if (Request::isPost()){
            $data = input('post.');
            $where['aid'] = $this->admin['id'];
            upDate('siteconfig',$where,$data);
            ajaxMsg(1,'更改成功');
        }
    }

    // 轮播图
    public function banner()
    {

        $data = selectData('banner','aid='.$this->admin['id']);
        $this->assign('data',$data);
        return $this->fetch();
    }

    public function addBanner()
    {
        if (Request::isPost()){
            $file = request()->file('banner');
            $res = Upload($file,['ext' =>'jpg,png'],'/uploads/banner/');

            insertData('banner',['aid'=>$this->admin['id'],'banner'=>$res['filePath'],'time'=>time()]);
            ajaxMsg(1,$res['filePath']);
        }
        return $this->fetch('addBanner');
    }

    public function delBanner()
    {
        $id = input('id');
        $banner = findData('banner','id='.$id);
        $url = $_SERVER['HTTP_HOST'];
        $imgUrl = str_replace('http://'.$url,'',$banner['banner']);
        // 删除文件并删除记录
        unlink('.'.$imgUrl);
        $res = delData('banner','id='.$id);
        if ($res)ajaxMsg(1,'操作成功');
        ajaxMsg(0,'文件可能已经丢失,删除失败');

    }

    // 个人设置
    public function userInfo()
    {
        return $this->fetch('editUserInfo');
    }
    // 修改个人
    public function editUserInfo()
    {
        if (Request::isPost()){
            $admin = input('post.');
            if ($admin['password']) $admin['password'] = sha1($admin['password']);
            else unset($admin['password']);
            $where['id'] = $admin['id'];
            $res = upDate('admin',$where,$admin);
            if ($res){
                $admin = findData('admin','id='.$admin['id']);

                Cookie::set('admin',$admin);
                ajaxMsg(1,'更改成功');
            }
            ajaxMsg(0,'更改失败');

        }
        $this->assign('admin',$this->admin);
        return $this->fetch('editUserInfo');
    }

    // 菜单列表
    public function menu()
    {
        $menu = getTrees(selectData('menu'));
        $this->assign('menu',$menu);
        return $this->fetch();
    }


    // 新增菜单
    public function addMenu()
    {
        if (Request::isPost()){
            $data = input('post.');
            $data['router'] = trim($data['router']);
            if ($data['pid'] == 0){
                $where['title']=$data['title'];
                if (findData('menu',$where)) ajaxMsg(0,'一级菜单,菜单名称不能重复');
            }
            insertData('menu',$data);
        }
        $menu = getTrees(selectData('menu'));
        $this->assign('menu',$menu);
        return $this->fetch('addMenu');
    }

    // 改变菜单状态
    public function changeStatus()
    {
        $where['id'] = input('id');
        $switch = input('status');

        if ($switch == "true") $data['status'] = 1;
        else $data['status'] = 0;

        $res = upDate('menu',$where,$data);
        if ($res)ajaxMsg(1,'更改成功');
        ajaxMsg(0,'网络不佳修改失败');
    }

    // 修改菜单
    public function editMenu()
    {
        if (Request::isPost()){
            $data = input('post.');
            $data['router'] = trim($data['router']);
            if ($data['pid'] == 0){
//                dump($data);die;
                $where1='title='."'".$data['title']."'".' and id !='.$data['id'];
//                dump($where);die;
                if (findData('menu',$where1)) ajaxMsg(0,'一级菜单,菜单名称不能重复');
            }

            $where['id'] = $data['id'];
            $res = upDate('menu',$where,$data);
            if ($res)ajaxMsg(1,'更改成功');
            ajaxMsg(0,'网络不佳修改失败');
        }
        $id = input('id');
        $where['id'] = $id;
        $data = findData('menu',$where);
        $menu = getTrees(selectData('menu'));
        $this->assign('menu',$menu);
        $this->assign('data',$data);
        return $this->fetch('editMenu');
    }

    // 删除菜单
    public function delMenu()
    {
        $id = input('id');
        $where['pid'] = $id;
        if (findData('menu',$where)) ajaxMsg(0,'该菜单还有下级菜单,不能直接删除');
        $where1['id'] = $id;
        $res = delData('menu',$where1);

        if ($res) ajaxMsg(1,'删除成功');
        ajaxMsg(0,'网络不佳,删除失败');
    }

    // 字体图标
    public function uniCode()
    {
        return $this->fetch('unicode');
    }








}